<?php
class Friend extends Model {
	function Friend(){
		parent::Model();
    }
    
    function isMyFriend($user_id,$friend_id){
    	$return = FALSE;
    	$query = "SELECT * FROM users_friend WHERE user_id='$user_id' AND friend_id='$friend_id' LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		$return = TRUE;
    	}
    	return $return;
    }
    
    function isWait($user_id,$friend_id){
    	$return = FALSE;
    	$query = "SELECT * FROM notif_req_friend WHERE user_id='$user_id' AND friend_id='$friend_id' AND finish='0' LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		$return = TRUE;
    	}else{
    		$query = "SELECT * FROM notif_req_friend WHERE user_id='$friend_id' AND friend_id='$user_id' AND finish='0' LIMIT 1";
	    	$query = $this->db->query($query);
	   	 	if($query->num_rows() > 0){
	   	 		$return = TRUE;
	    	}
    	}
    	return $return;
    }
    function addRequest($user_id,$friend_id){
    	$query = "SELECT * FROM notif_req_friend WHERE user_id='$user_id' AND friend_id='$friend_id' LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$id  = $row->id;
			}
   	 		$query = "UPDATE notif_req_friend  set finish='0' WHERE user_id='$user_id' AND friend_id='$friend_id'";
	    	if($query = $this->db->query($query)){
	    		return TRUE;
	    	}
    	}else{
	    	$query = "INSERT INTO notif_req_friend  (user_id,friend_id,finish) VALUES('$user_id','$friend_id','0')";
	    	if($query = $this->db->query($query)){
	    		return TRUE;
	    	}
    	}
    	
    	return FALSE;
    	
    }
    function setFriend($user_id,$friend_id){
    	$query = "INSERT INTO users_friend  (user_id,friend_id,date) VALUES('$user_id','$friend_id','".time()."')";
    	if($query = $this->db->query($query)){
	    	$query = "INSERT INTO users_friend  (user_id,friend_id,date) VALUES('$friend_id','$user_id','".time()."')";
	    	if($query = $this->db->query($query)){
	    		return TRUE;
	    	}
    	}
    	
    	return FALSE;
    }
    function removeThem($friend_id,$user_id){
    	$query = "DELETE FROM users_friend  WHERE user_id='$user_id' AND friend_id='$friend_id'";
    	if($query = $this->db->query($query)){
	    	$query = "DELETE FROM users_friend  WHERE user_id='$friend_id' AND friend_id='$user_id'";
	    	if($query = $this->db->query($query)){
	    		return TRUE;
	    	}
    	}
    }
}